﻿using InnoWeb.CEND.BL.Service.Videos;
using InnoWeb.CEND.Core.Model.Videos;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.UI.MVC.Areas.Admin.Models;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class VideoController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<VideoContents> _videoContentRepository;
        private static VideoContentService _videoService;

        public VideoController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _videoContentRepository = new GenericRepository<VideoContents>(_context);

            _videoService = new VideoContentService(_unitOfWork, _videoContentRepository);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            var itemList = _videoService.GetAllElements().ToList();

            return View(itemList);
        }

        public ActionResult Details(int id)
        {
            var obj = _videoService.GetElementById(id);
            return View(obj);
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(VideoInfoModel model)
        {
            try
            {
                //WebImage
                //var img = WebImage.GetImageFromRequest();
                var video = Request.Files["fileUpload"];

                string fileName = Guid.NewGuid().ToString().Replace("-","");
                string serverPath = Server.MapPath("~/Content/Videos");

                string fullPath = Server.MapPath("~/Content/Videos/");

                VideoUploader.VideoSave(fullPath, video, fileName);

                var expert = model.VideoContent;
                expert.VideoUrl =  fileName + ".avi";
                expert.ModifiedDate = DateTime.Today;
                expert.CreatedDate = DateTime.Today;
                expert.IsActive = true;
                expert.IsDeleted = false;
                _videoService.CreateElement(expert);
                _videoService.SaveChanges();

                Response.Write("<script type='text/javascript'>confirm(\"Data successfully added to database.\"); document.location = 'Index';</script>");

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Edit(int id)
        {
            var obj = _videoService.GetElementById(id);
            return View(new VideoInfoModel { VideoContent = obj });
        }

        [HttpPost]
        public ActionResult Edit(int id, VideoInfoModel model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                var expert = model.VideoContent;
                expert.ModifiedDate = DateTime.Today;

                if (img.ContentLength >0)
                {
                    string fileName = Guid.NewGuid().ToString().Replace("-", "");
                    string serverPath = Server.MapPath("~/Content/Videos");

                    string fullPath = Server.MapPath("~/Content/Videos/Full/");

                    VideoUploader.VideoSave(fullPath, img, fileName);


                    expert.VideoUrl = fileName + ".avi";
                }
                else
                {
                    expert.VideoUrl = _videoService.GetAllElements().Where(a => a.Id == model.VideoContent.Id).FirstOrDefault().VideoUrl; 
                }

                _videoService.UpdateElement(expert);
                _videoService.SaveChanges();

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Delete(int id)
        {
            var obj = _videoService.GetElementById(id);
            return View(obj);
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var obj = _videoService.GetElementById(id);
                obj.IsDeleted = true;
                _videoService.UpdateElement(obj);
                _videoService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
